#!/usr/bin/env python3
"""WebSocket服务器 API 客户端示例"""

import requests

def get_devices(api_url="http://localhost:8767/api/devices"):
    """获取所有在线设备信息"""
    try:
        response = requests.get(api_url, timeout=5)
        if response.status_code == 200:
            return response.json()
        print(f"HTTP错误: {response.status_code}")
    except Exception as e:
        print(f"请求错误: {e}")
    return None

def send_message(device_id, message, timeout=30, api_url="http://localhost:8767/api/send_message"):
    """发送消息到指定设备并等待回复"""
    try:
        payload = {
            "device_id": device_id,
            "message": message,
            "timeout": timeout
        }
        
        response = requests.post(api_url, json=payload, timeout=timeout+5)
        
        if response.status_code == 200:
            return response.json()
        else:
            print(f"错误: {response.status_code}, {response.text}")
            
    except Exception as e:
        print(f"请求错误: {e}")
    
    return None

if __name__ == "__main__":
    # 获取设备示例
    devices = get_devices()
    print("设备列表:", devices)
    
